package edu.uic.cs.i422.s2011.team4;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Form extends Activity implements OnClickListener {
	
	static final public int DIALOG_RESET = 0;
	
	private AlertDialog dialog;
	
    /** Called when the activity is first created. */	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.form);
        
        /**
         * Create our Activity layout container
         */
        LinearLayout container = new LinearLayout(this);
        container.setOrientation(LinearLayout.VERTICAL);
        container.setGravity(Gravity.CENTER);

        TextView textTitle = (TextView) findViewById(R.id.formSiteName);
        textTitle.setText(SquirrelData.selectedSite);
        
        TextView changeSite = (TextView) findViewById(R.id.formChangeSite);
        changeSite.setOnClickListener(this);
		
        /**
         * Email : TODO Indicator
         */
        Button email = (Button) findViewById(R.id.formEmail);
        email.setOnClickListener(this);           
        
        /**
         * Date and Time
         */
        Button dateTime = (Button) findViewById(R.id.formDateTime);
        dateTime.setOnClickListener(this);
        
        /**
         * Squirrels
         */
        Button squirrels = (Button) findViewById(R.id.formSquirrels);
        squirrels.setOnClickListener(this);        
        
        /**
         * Predators
         */
        Button predators = (Button) findViewById(R.id.formPredators);
        predators.setOnClickListener(this);        
        
        /**
         * Comments
         */
        Button comments = (Button) findViewById(R.id.formComments);
        comments.setOnClickListener(this);        
        
        /**
         * Submit and Reset
         */
        Button submit = (Button) findViewById(R.id.formSubmit);
        submit.setOnClickListener(this);
        Button reset = (Button) findViewById(R.id.formReset);
        reset.setOnClickListener(this);
        
        /**
         * Check Marks
         */
        checkMarks();
        
    }
    
    public void onClick(View v)  {
    	/**
    	 * Create new intent and call our class!
    	 */

    	Intent myIntent = null;
    	
    	switch(v.getId()) {
    		case R.id.formChangeSite :
    			myIntent = new Intent(v.getContext(), SiteListing.class);
    			break;
    		case R.id.formEmail :
    			myIntent=new Intent(v.getContext(), EmailActivity.class);
    			break;
    		case R.id.formDateTime :
    			myIntent = new Intent(v.getContext(), DateTimeDisplay.class);
    			break;
    		case R.id.formSquirrels :
    			myIntent=new Intent(v.getContext(), SquirrelActivity.class);
    			break;
    		case R.id.formPredators :
    			myIntent= new Intent(v.getContext(), PredatorObservation.class);
    			break;
    		case R.id.formComments :
    			myIntent = new Intent(v.getContext(), CommentData.class);
    			break;
    		case R.id.formSubmit :
    			if (SquirrelData.isAllChecked()) {
    				SquirrelData.writeToCSV();
    				myIntent = new Intent(v.getContext(), ThankYou.class);
    			}
    			//finish();
    			break;	
    		case R.id.formReset :
    			showDialog(DIALOG_RESET);
    			break;
    	}
    	
    	// Mike - removed the startActivityOnResult to insure not extra duplicate activities are 
    	// created
    	if (myIntent != null) {
	    	startActivityForResult(myIntent, 0);
    	}
    }
    
    public Dialog onCreateDialog(int id) {
    	switch(id) {
    		case DIALOG_RESET :
    			/**
    			 * First create our builder and inflate our dialog from layout.
    			 */
    			AlertDialog.Builder builder = new AlertDialog.Builder(this);
    			LinearLayout layout = (LinearLayout) View.inflate(this, R.layout.squirrel_dialog, (ViewGroup) findViewById(R.id.dialogRoot));
    			    			
    			/**
    			 * Set the Text for our costume layout
    			 */    			 
				TextView title = (TextView) layout.findViewById(R.id.dialogTitle);
				title.setText(R.string.formDialogTitle);
				TextView text = (TextView) layout.findViewById(R.id.dialogText);
				text.setText(R.string.formDialogText);    			 
    			 
    			 /**
    			  * Set the buttons
    			  */
    			 Button yes = (Button) layout.findViewById(R.id.dialogYes);
    			 Button no = (Button) layout.findViewById(R.id.dialogNo);
    			 
    			 yes.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						//TODO Call Squirrel Data to SUBMIT
						finish();
					}
    			 });
    			 
    			 no.setOnClickListener(new OnClickListener() {
    				 public void onClick(View v) {
    					//TODO Call Squirrel Data to ERASE
    					dialog.cancel();
    				 }
     			 });
    			
    			
    			/**
    			 * Create the dialog, set view, set title, Show the dialog
    			 */
    			builder.setView(layout);		
    			dialog = builder.create();    			
    			break;
    	}
    	return dialog;
    }
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	checkMarks();
    }
    
    /**
     * Check Marks
     */
    private void checkMarks() {
    	/**
    	 * Restore all the indicators
    	 */
    	ImageView emailIndicator = (ImageView) findViewById(R.id.formEmailIndicator);
        if (SquirrelData.getEmailCheckMark()) {
        	emailIndicator.setImageResource(R.drawable.squirrel_yes);
        }
    	ImageView dateTimeIndicator = (ImageView) findViewById(R.id.formDateTimeIndicator);
        if (SquirrelData.getDateTimeCheckMark()) {
        	dateTimeIndicator.setImageResource(R.drawable.squirrel_yes);
        }
        ImageView squirrelsIndicator = (ImageView) findViewById(R.id.formSquirrelsIndicator);
        if (SquirrelData.getSquirrelsCheckMark()) {
        	squirrelsIndicator.setImageResource(R.drawable.squirrel_yes);
        }
        ImageView predatorsIndicator = (ImageView) findViewById(R.id.formPredatorsIndicator);
        if (SquirrelData.getPredatorsCheckMark()) {
        	predatorsIndicator.setImageResource(R.drawable.squirrel_yes);
        }
        ImageView commentsIndicator = (ImageView) findViewById(R.id.formCommentsIndicator);
        if (SquirrelData.getCommentsCheckMark()) {
        	commentsIndicator.setImageResource(R.drawable.squirrel_yes);
        }	
    }
}